package com.flk.flkelasticsearch.controller;

import com.flk.flkelasticsearch.entity.EsNews;
import com.flk.flkelasticsearch.es.EsInfomation;
import com.flk.flkelasticsearch.service.IFlkNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.common.util.DateUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xxwy
 * on 2018/11/13 0013
 */
@RestController
@RequestMapping("/news/")
public class FlkNewsController {

    @Autowired
    IFlkNewsService iFlkNewsService;

    @RequestMapping(value = "hintInfo",produces = {"application/json;charset=UTF-8"})
    public ResponseEntity getHintInfo(@RequestParam String info) throws IOException {
        Map<String, Object> returnMap = new HashMap<>();
        List<String> title = iFlkNewsService.getHotInfo(info);
        returnMap.put("data",title);
        return ResponseEntity.ok(returnMap);
    }

    @RequestMapping(value = "news_list",produces = {"application/json;charset=UTF-8"})
    public Map<String, Object> getNewsInfo(@RequestParam String info, @RequestParam int size, @RequestParam() int page){
        Map<String, Object> returnMap = new HashMap<>();
        List<EsNews> titleAndCode = iFlkNewsService.getTitleAndCode(info, size, page);
        //维护热词库
        if(titleAndCode != null && titleAndCode.size() > 0){
            iFlkNewsService.saveOrUpdateHotWords(info,EsInfomation.FLX_HOTWORD_NEWS);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        Date now = new Date();
        for (EsNews esNews: titleAndCode) {
            esNews.setNewsId(Integer.parseInt(esNews.getId()));
            String createTime = esNews.getCreateTime();
            try {
                Date date = sdf.parse(createTime);
                Map<String, Object> datePoor = DateUtil.getDatePoor(now, date);
                String createNews = datePoorTransToMsg(datePoor,date);
                esNews.setCreateTime(sdf2.format(date));
                esNews.setCreateNews(createNews);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        returnMap.put("data",titleAndCode);
        return returnMap;
    }

    private String datePoorTransToMsg(Map<String, Object> datePoor, Date createTime) {
        String str = "";
        long day = (long) datePoor.get("day");
        long hour = (long) datePoor.get("hour");
        long min = (long) datePoor.get("min");

        if (day > 0) {
            if (day >= 5) {
                // 时间间隔大于5天 前台显示具体发布时间
                str = createTime.toString().substring(0, createTime.toString().length() - 2);
                return str;
            }

            str = day + "天前发布";
            return str;
        }
        if (hour > 0) {
            str = hour + "小时前发布";
            return str;
        }
        if (min <= 0) {
            str = "刚刚发布";
            return str;
        } else {
            str = min + "分钟前发布";
            return str;
        }
    }
}
